#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from Ui_dialog_AE_translation import *

class dialog_AE_translation(QtGui.QDialog, Ui_Dialog_AE_translation):
    
    
    def __init__(self,window_parent, ontology, translation):
        super(dialog_AE_translation,self).__init__(window_parent)
        
        self.setupUi(self)
        self.parent=window_parent
        self.ontology = ontology
        self.translation = translation
        self.translation_name.setText(QtGui.QApplication.translate("dialog_AE_translation", str(translation), None, QtGui.QApplication.UnicodeUTF8))
        self.all_translated = False
        
        for attribute_type in self.ontology.all_attributes_type :
            if self.ontology.all_attributes_type[attribute_type].name == translation :
                self.translation_attribute_type_id = self.ontology.all_attributes_type[attribute_type].attribute_type_id
        
        
        self.untranslated_AEs = []
        total_AEs = 0
        
        for AE_id in self.ontology.all_AEs :
                total_AEs += 1
                attributes = self.ontology.all_AEs[AE_id].getOwnAttributes()
                if len(attributes) > 0 :
                    existing_language_attribute_for_AE = False
                    for att in attributes :
                        if att.attribute_type_id == self.translation_attribute_type_id :
                            existing_language_attribute_for_AE = True
                    if not existing_language_attribute_for_AE :
                            self.untranslated_AEs.append(AE_id)
                            
                else :
                    self.untranslated_AEs.append(AE_id)
        
            

        #self.connect(self.pushButton_next, QtCore.SIGNAL("accepted()"),self, QtCore.SLOT("accept()"))
        #self.connect(self.pushButton_stop, QtCore.SIGNAL("rejected()"),self, QtCore.SLOT("reject()"))
        
        
        self.init_AE()
        
        
    
    def init_AE(self):
        self.AE_id = self.untranslated_AEs[0] #the first Ae of the list of the untranslated AEs
        name = self.ontology.all_AEs[self.AE_id].name
        definition = self.ontology.all_AEs[self.AE_id].definition
        self.lineEdit_Latin_name.setText(QtCore.QString.fromUtf8(str(name)))
        self.textEdit_Definition.setText(QtCore.QString.fromUtf8(str(definition)))
        self.lineEdit_translation.setText("")
        
                             

    @QtCore.pyqtSignature("bool")
    def on_pushButton_next_clicked(self):
        self.translation = unicode(self.lineEdit_translation.text()).encode('utf-8')
        self.translation = self.translation.capitalize()
        if len(self.translation) > 0 :
            self.ontology.add_attribute(self.translation_attribute_type_id,self.AE_id,self.translation)
            self.untranslated_AEs.remove(self.AE_id)
            if len(self.untranslated_AEs)>0 :
                self.init_AE()
            else :
                self.all_translated = True
                QtGui.QMessageBox.information(self,"myCF information","all AEs are now translated in the selected language.")
                QtGui.QDialog.accept(self) 
            
        else :    
            QtGui.QMessageBox.warning(self,"myCF error","You must enter a translation for this AE.")      
                 
 
    @QtCore.pyqtSignature("bool")
    def on_pushButton_stop_clicked(self):
        QtGui.QMessageBox.warning(self,"myCF information","All AEs have not been translated")
        QtGui.QDialog.reject(self)
               